package com.haoshuang.zhouzhoubang.ui.page.mine.notice.list

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.config.URLConstants
import com.haoshuang.zhouzhoubang.databinding.ActivityNoticeListBinding
import com.haoshuang.zhouzhoubang.model.bean.HomeDataBean
import com.haoshuang.zhouzhoubang.ui.page.help.detail.HelpDetailActivity
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.finishAll
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.view.adapter
import com.mufeng.libs.utils.view.vertical

/**
 *  
 */
class NoticeListActivity : BaseVMActivity<NoticeListViewModel, ActivityNoticeListBinding>() {

    companion object {
        fun launch(context: Context){
            context.startPage<NoticeListActivity>()
        }
    }

    private val data = arrayListOf<HomeDataBean.SystemNotice>()
    private val adapter = NoticeListAdapter(data)

    override fun initView(savedInstanceState: Bundle?) {
        binding.apply{
            titleBar.title = "平台公告"
            titleBar.leftImageView().click { finish() }

            recyclerView.vertical()
                .adapter(adapter)

            adapter.setEmptyData()

            adapter.setOnItemClick { view, position, item ->
                startPage<HelpDetailActivity>(
                    "title" to item?.title,
                    "url" to "${URLConstants.baseUrl}${item?.detail_url}"
                )
            }

            refreshLayout.setOnRefreshListener {
                vm.getNoticeList()
            }
        }
    }

    override fun initData() {
        vm.getNoticeList()
    }

    override fun startObserve() {
        vm.getNoticeListData.observeState(this){
            onError { code, msg ->
                binding.refreshLayout.finishAll()
            }
            onSuccess {
                data.clear()
                data.addAll(it?: mutableListOf())
                adapter.notifyDataSetChanged()
                binding.refreshLayout.finishAll()
            }
        }
    }

}